from django.test import TestCase
from django.contrib.auth.models import User
from apps.schedule.models import Event

# Create your tests here.

class EventTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(
            username='testuser',
            password='testpass'
        )
        self.client.force_authenticate(user=self.user)

    def test_create_event(self):
        """测试创建日程"""
        data = {
            'title': '测试日程',
            'description': '测试描述',
            'date': '2024-03-20',
            'start_time': '14:30',
            'end_time': '16:00',
            'location': '会议室',
            'importance': 'high',
            'reminder': True,
            'reminder_time': '2024-03-20T14:15:00'
        }
        
        response = self.client.post('/api/schedule/events/', data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['status'], 'success')
        
        # 验证创建的日程
        event = Event.objects.get(id=response.data['data']['id'])
        self.assertEqual(event.title, data['title'])
        self.assertEqual(event.user, self.user)
        # 验证创建者被自动添加为相关用户
        self.assertTrue(event.related_users.filter(id=self.user.id).exists())

    def test_share_event(self):
        """测试分享日程"""
        # 创建测试日程
        event = Event.objects.create(
            user=self.user,
            title='测试日程',
            date='2024-03-20',
            start_time='14:30',
            end_time='16:00'
        )
        
        # 创建要分享的用户
        share_user = User.objects.create_user(
            username='shareuser',
            password='sharepass'
        )
        
        # 测试分享功能
        response = self.client.post(
            f'/api/schedule/events/{event.id}/share/',
            {'user_ids': [share_user.id]}
        )
        
        self.assertEqual(response.status_code, 200)
        self.assertTrue(
            event.related_users.filter(id=share_user.id).exists()
        )
